<template>
  <div>
    <el-table v-loading="loading" :data="driverList">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="起始地" align="center" prop="departureId" width="200">
        <template slot-scope="scope">
          {{scope.row.departure.district+scope.row.departure.town+scope.row.departure.warehouseName}}
        </template>
      </el-table-column>
      <el-table-column label="目的地" align="center" prop="destinationId" width="200">
        <template slot-scope="scope">
          {{scope.row.destination.district+scope.row.destination.town+scope.row.destination.warehouseName}}
        </template>
      </el-table-column>
      <el-table-column label="运输类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.transport_task_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="总重量" align="center" prop="weight" />
      <el-table-column label="运输状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.transport_task_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['transport:driver:change']"
            v-if="scope.row.state === 1 || scope.row.state === 2"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { changeState, getTaskInfo, getTaskList } from '@/api/driver/driverTask'

export default {
  name: 'DriverTask',
  dicts: ['transport_task_type','transport_task_state'],
  data(){
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运输任务表格数据
      driverList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 运输状态时间范围
      daterangeCreateTime: [],
      // 运输状态时间范围
      daterangeUpdateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: 0,
        managerId: 0,
        departureId: 0,
        destinationId: 0,
        state: 0,
        type: 0,
        weight: 0,
        isDelete: 0,
        createBy: 0,
        createTime: null,
        updateBy: 0,
        updateTime: null,
        remark: null,
      },
      // 表单参数
      form: {},
    }
  },

  created() {
    this.getList();
  },

  methods:{
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeUpdateTime && '' != this.daterangeUpdateTime) {
        this.queryParams.params["beginUpdateTime"] = this.daterangeUpdateTime[0];
        this.queryParams.params["endUpdateTime"] = this.daterangeUpdateTime[1];
      }
      getTaskList(this.queryParams).then(response => {
        this.driverList = response.rows;
        console.log(this.driverList)
        this.total = response.total;
        this.loading = false;
      });
    },

    //修改运输状态的方法
    handleUpdate(row){
      if (row.state === 1){
        row.state = 2
      }
      else{
        row.state = 3
      }
      let data = {
        taskId: row.taskId,
        state:  row.state
      }
      changeState(data).then(response => {
        if (response.code === 200) {
          this.$modal.msgSuccess("修改成功");
          this.getList();
        }
      })
    },

    viewMessage(row){
      getTaskInfo(row.taskId).then(respond => {
        console.log(respond.data)
      })
    }
  }
}
</script>

<style scoped>

</style>
